﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

namespace WebApp
{
    public partial class Register : System.Web.UI.Page
    {
        private UserManager m_UserManager = new UserManager();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (null != this.m_UserManager.GetUserByName(this.txtUserName.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('该用户名已经注册!')", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtPhone.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('手机/电话号码不能为空!');", true);
                return;
            }
            if (txtPhone.Text.Length != 11)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Register Failed", "alert('手机号码为11位数字！请正确填写!')", true);
                return;
            }

            if (string.IsNullOrEmpty(this.txtMailBox.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('邮箱不能为空!');", true);
                return;
            }
            if (this.txtPassword.Text == "")
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('密码不能为空!');", true);
                return;
            }
            if (!IsEmail(this.txtMailBox.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请输入正确的Email!');", true);
                return;
            }


            User theNewUser = new User();
            theNewUser.UserId = Guid.NewGuid();
            theNewUser.Email = this.txtMailBox.Text.Trim();
            theNewUser.UserName = this.txtUserName.Text.Trim();
            theNewUser.Password = this.txtPassword.Text;
            theNewUser.tel = this.txtPhone.Text.Trim();
            theNewUser.HowToKnowIbeauty = "百度";
            theNewUser.Level = "普通会员";
            theNewUser.RegisterTime = DateTime.Now;
            theNewUser.Status = true;//非激活用户
            this.m_UserManager.Insert(theNewUser);
            //Session["SignInUser"] = theNewUser;
            //Session["userid"] = theNewUser.UserId;
            //Session["tel"] = theNewUser.tel;
            //Session["password"] = theNewUser.Password;
            //tools m = new tools();
            //User order = m.CreateOrder();
            //Control mailTemplate = LoadControl("youjian.ascx");
            //((youjian)mailTemplate).users = order;
            //StringBuilder stringBuilder = new StringBuilder();
            //StringWriter stringWriter = new StringWriter(stringBuilder);
            //HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            //mailTemplate.RenderControl(htmlTextWriter);
            //htmlTextWriter.Close();
            //SmtpClient theSmtp = new SmtpClient();
            //theSmtp.Host = mailServer;
            //theSmtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //theSmtp.Port = 25;
            //theSmtp.UseDefaultCredentials = false;
            //theSmtp.Credentials = new System.Net.NetworkCredential(mailUserName, mailPassword);
            //MailMessage theMail = new MailMessage();
            ////获取自己的邮箱地址
            //theMail.From = new MailAddress(mailUserName);
            //theMail.To.Add(new MailAddress(this.txtemail.Text));
            //theMail.Subject = "来自JJ-9信函：原装进口葡萄酒在线商店_中国首创海外酒庄葡萄酒定制_澳洲葡萄酒_澳大利亚葡萄酒会员注册确认。";
            //theMail.Priority = MailPriority.High;
            //theMail.BodyEncoding = System.Text.Encoding.UTF8;
            //theMail.IsBodyHtml = true;
            //string body = "感谢您的注册，您注册的用户名是：'" + theNewUser.tel + "' 邮箱是：'" + theNewUser.Email + "'";
            //theMail.Body = body;
            ////theMail.Body = stringBuilder.ToString();
            //try
            //{
            //    theSmtp.Send(theMail);
            //}
            //catch (Exception ex)
            //{
            //}
            Session["SignInUser"] = theNewUser;
            Session["userid"] = theNewUser.UserId;
            Session["tel"] = theNewUser.tel;
            Session["UserName"] = theNewUser.UserName;
            Session["email"] = theNewUser.Email;
            Session["password"] = theNewUser.Password;
            Response.Redirect("~/RegisterOK.aspx");
        }

        public bool IsEmail(string str_Email)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_Email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        }
    }
}